﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Vigenere
{
    /// <summary>
    /// 创建一个加密类
    /// </summary>
    public class MyEncoder
    {
        /// <summary>
        /// 明文
        /// </summary>
        public string PlainText { get; set; }

        /// 密钥
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 密文
        /// </summary>
        public string CipherText { get; set; }

        /// <summary>
        /// 加密动作
        /// </summary>
        public void Encode()
        {
            int numkey;
            //listnum储存key的整数形式
            List<int> listnum = new List<int>();
            for (int i = 0; i < Key.Length; i++)
            {
                numkey = Key[i] - 'a';
                listnum.Add(numkey);
            }

            for (int i = 0; i < PlainText.Length; i++)
            {
                //明文字母转换为整数形式
                int numplain = PlainText[i] - 'a';
                //计算密文字母的整数形式
                int n = (i % Key.Length);
                int numcipher = numplain + listnum[n];
                //密文转换为字母形式
                CipherText += Convert.ToChar(numcipher + 'a');
            }
        }
    }
}